<template>
    <div>
      <AplidForm
        ref="formRef"
        v-bind="formConfig"
        v-model="formModel"
        @submit="onSubmit">
      </AplidForm>
    </div>
  </template>
  
  <script setup lang="ts">
  import apiMod from '@/apiMod/index'
  import { formConfig } from '../config'
  // import { useCommonStore } from '@/stores/common'
  import { getAreaObj } from '@/utils/util'
  import {EncodeUtf8} from '@/views/oneStopOffice/utils/index'
  let emit = defineEmits(['refresh'])

  // import { useOperate } from '@/views/datacenter/hooks/useConfig'

  type Props = {
    nowRowData?:any
  }
  let props = withDefaults(defineProps<Props>(),{
    nowRowData:{}
  })
  let formRef = ref()
  let formModel = ref()
  const onSubmit = (form, done) => {
        if(form.policyContent){
			form.policyContent = EncodeUtf8(form.policyContent)
		}
		if(form.policyKeywords){
			form.policyKeywords = form.policyKeywords.join(',')
		}
		// 分开适应老人年龄
		if(form.ageRange && form.ageRange!=''){
			let list = form.ageRange.split(',')
			form.minAge = list[0] == ''?undefined:list[0]
			form.maxAge = list[1] == ''?undefined:list[1]
		}else{
			form.minAge = undefined
			form.maxAge = undefined
		}
      const areasObj: any = getAreaObj(form.areas, 'from')
			const areasNameObj: any = getAreaObj(form.areasName, 'from')
			
			form.policyLevel = 1
			form.policyLevelLabel = '国家级'
			// form.module = '1'
			// form.moduleLabel = '一站办'
			// 一站办处理
			if(Array.isArray(form.adaptiveHierarchy) && form.adaptiveHierarchy.length>0){
				if(form.adaptiveHierarchy.length==1){
					form.adaptiveHierarchyCity = form.adaptiveHierarchy[0]
					form.adaptiveHierarchyArea = ''
				}else{
					form.adaptiveHierarchyCity = form.adaptiveHierarchy[0]
					form.adaptiveHierarchyArea = form.adaptiveHierarchy[1]
				}
       
        form.adaptiveHierarchy = form.adaptiveHierarchy.join(',')
        
        if (form.adaptiveHierarchyLabel) {
          
          const temp = form.adaptiveHierarchyLabel.split('/')
          form.adaptiveHierarchyLabel = temp[temp.length - 1]
        }
			}
			
			const params = {
				...form,
				...areasObj,
				...areasNameObj,
				areas: undefined,
				areasName: undefined,
				code: 3011,
			}
      // 输入框都进行一下加密
      formConfig.column.forEach(el=>{
        if((!el.type||el.type=='input'||el.type=="text")&& params[el.prop]){
          params[el.prop] =  EncodeUtf8(params[el.prop])
        }
      })
    apiMod.view_dpPolicyDocument_gather(params).then(res=>{
      done()
      emit('refresh')
    })
  }
  const onSave = () => {
    formRef.value!.onSubmit()
  }
  defineExpose({
    onSave
  })
  </script>
  
  <style scoped></style>
  